package offer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author SSK
 * @Date Created in 22:58 2019/8/4
 * @Description 正则表达式匹配
 * @Version 1.0
 */
public class T53 {
    public boolean match(char[] str, char[] pattern) {
        if (str == null && pattern == null) {
            return true;
        }
        if (str==null || pattern == null) {
            return false;
        }
        return match(str,0,pattern,0);
    }
    public boolean match(char[] str, int sindex, char[] pattern, int pindex) {
        if (pindex==pattern.length&&sindex==str.length) {
            return true;
        }else if (sindex==str.length) {
            if ((pattern.length-pindex)%2==1) {
                return false;
            }
            for (int i = pindex+1; i < pattern.length; i+=2) {
                if (pattern[i]!='*') {
                    return false;
                }
                return true;
            }
        }else if (pindex==pattern.length) {
            return false;
        }
        if (pindex < pattern.length-1 && pattern[pindex+1] == '*') {
            boolean flag = match(str,sindex,pattern,pindex+2);
            for (int i = sindex; i < str.length; i++) {
                if (str[i]==pattern[pindex] || pattern[pindex]=='.') {
                    flag = flag || match(str,i+1,pattern,pindex+2);
                } else {
                    break;
                }
            }
            return flag;
        }
        if (str[sindex]==pattern[pindex] || pattern[pindex]=='.') {
            return match(str,sindex+1,pattern,pindex+1);
        }
        return false;
    }
    public boolean match2(char[] str, char[] pattern) {
        Pattern pattern1 = Pattern.compile(new String(pattern));
        return pattern1.matcher(new String(str)).matches();

    }
    public static void main ( String[] args) {
        System.out.println(new T53().match2("aaba".toCharArray(),"ab*a*c*a".toCharArray()));
    }
}
